//package com.ssy.lingxi.gateway.handler.response;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.ssy.common.response.ResponseCode;
//import com.ssy.common.response.Wrapper;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpHeaders;
//import org.springframework.web.server.ServerWebExchange;
//import org.synchronoss.cloud.nio.multipart.util.IOUtils;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.io.IOException;
///**
//* 自定义response数据
//* @author wrc
//* @date 2020/6/8
//*/
//@Configuration
//public class ResponseGlobalFilter implements GlobalFilter, Ordered {
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange,
//                             GatewayFilterChain chain) {
//        //如果是获取验证码的请求，则跳出
//        if(exchange.getRequest().getPath().value().contains("captcha")){
//            return chain.filter(exchange);
//        }
//        //构建响应拦截处理器
//        BodyHandlerFunction bodyHandler = (resp, body) -> Flux.from(body)
//                .map(dataBuffer -> {
//                    //响应信息转换为字符串
//                    String reqBody = null;
//                    try {
//                        //dataBuffer 转换为String
//                        reqBody = IOUtils
//                                .inputStreamAsString(dataBuffer.asInputStream(), "UTF-8")
//                                .replaceAll(">\\s{1,}<", "><");
//                        //TODO 下层服务报错在这里处理
//                        if(reqBody.contains("status=500")) {
//                            ObjectMapper mapper=new ObjectMapper();
//                            reqBody = mapper.writeValueAsString(Wrapper.fail(ResponseCode.BUSINESS_ERROR));
//                        }
//                        if(reqBody.contains("status=404")) {
//                            ObjectMapper mapper=new ObjectMapper();
//                            reqBody = mapper.writeValueAsString(Wrapper.fail(ResponseCode.SERVICE_NOT_EXIST));
//                        }
//
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                    return reqBody;
//                })
//                .flatMap(orgBody -> {
//                    String rbody = orgBody + "";
//                    return resp.writeWith(Flux.just(rbody)
//                            .map(bx -> resp.bufferFactory()
//                                    .wrap(bx.getBytes())));
//                }).then();
//        //构建响应包装类
//        BodyHandlerServerHttpResponseDecorator responseDecorator = new BodyHandlerServerHttpResponseDecorator(
//                bodyHandler, exchange.getResponse());
//        return chain
//                .filter(exchange.mutate().response(responseDecorator).build());
//    }
//
//    @Override
//    public int getOrder() {
//        //WRITE_RESPONSE_FILTER 之前执行
//        return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1;
//    }
//
//}
